package com.ruoyi.waterManagement.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.business.BusinessUtils;
import com.ruoyi.waterManagement.domain.vo.DataQualityVO;
import com.ruoyi.waterManagement.domain.vo.RzsWrStatvolHwrVO;
import com.ruoyi.waterManagement.mapper.DataQualityMapper;
import com.ruoyi.waterManagement.mapper.WrStatvolHMapper;
import com.ruoyi.waterManagement.service.IDataQualityService;
import com.ruoyi.waterManagement.service.IWrStatvolHService;
import com.ruoyi.waterManagement.util.ArithmeticUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

import static org.apache.commons.lang3.time.DateUtils.addDays;
import static org.apache.commons.lang3.time.DateUtils.truncate;


/**
 * 数据质量完整率记录Service业务层处理
 * 
 * @author hwd
 * @date 2023-02-16
 */
@Service
public class DataQualityServiceImpl extends ServiceImpl<DataQualityMapper, DataQualityVO> implements IDataQualityService
{
    @Autowired
    private DataQualityMapper dataQualityMapper;
    @Autowired
    private WrStatvolHMapper wrStatvolHMapper;

    @Override
    public List<DataQualityVO> listStationDataWzl(DataQualityVO dataQualityVO) throws ParseException {

        //如果没有选择时间，默认为当前年月
        if (dataQualityVO.getQueryTime()==null){
            dataQualityVO.setQueryTime(new Date());
        }
        String [] strings= DateUtils.jsYmd(dataQualityVO.getQueryTime());
        String yy =strings[0];
        String mm =strings[1];
        dataQualityVO.setYy(yy);
        dataQualityVO.setMm(mm);

        //查询水管机构级别
        HashMap<String, Object> addvAndOrgCodeInfo = BusinessUtils.getAddvAndOrgCodeInfo(dataQualityVO.getOrgCd());
            int grade = (int) addvAndOrgCodeInfo.get("grade");
            String resCode = (String) addvAndOrgCodeInfo.get("resCode");
            dataQualityVO.setOrgCd(resCode + "%");
        if (grade == 1) {
            //查找下属水管机构
            resCode = resCode + "%0000";
        } else if (grade == 2) {
            //查找下属水管机构
            resCode = resCode + "%00";
        } else if (grade == 3) {
            resCode = resCode + "%";
        } else {
            resCode = resCode;
        }
        //根据水管机构和时间查询测站实报次数，上报频率
        List<DataQualityVO> dataQualityVOList= dataQualityMapper.selStStationRSbCount(dataQualityVO);
        //查找下属机构
        RzsWrStatvolHwrVO rzsWrStatvolHwrVO=new RzsWrStatvolHwrVO();
        rzsWrStatvolHwrVO.setOrgCd(resCode);
        List<RzsWrStatvolHwrVO> orglist = wrStatvolHMapper.listOrgCd(rzsWrStatvolHwrVO);
            List<DataQualityVO> rateVoList=new ArrayList<>();
            for (RzsWrStatvolHwrVO r:orglist
                 ) {
                HashMap<String, Object> stringObjectHashMap = BusinessUtils.getAddvAndOrgCodeInfo(r.getOrgCd());
                String ro =  (String) stringObjectHashMap.get("resCode");
                //过滤后只查所属水管机构的测站
                List<DataQualityVO> filterDataQualityVOList = dataQualityVOList.stream().filter(s -> s.getOrgCd().startsWith(ro)).collect(Collectors.toList());
                DataQualityVO newDataQualityVO=new DataQualityVO();
                int sbCount =0;
                int ybCount =0;
                for (DataQualityVO d:filterDataQualityVOList
                     ) {
                    sbCount+=d.getSbCount();
                    //通过查询时间和上报频次计算应报次数
                    ybCount+=countEQ(dataQualityVO.getQueryTime(),d.getStatFreq());
                }
                newDataQualityVO.setSbCount(sbCount);
                newDataQualityVO.setYbCount(ybCount);
                newDataQualityVO.setStatCd(r.getStatCd());
                newDataQualityVO.setOrgCd(r.getOrgCd());
                newDataQualityVO.setOrgNm(r.getOrgNm());
                if (ybCount==0){
                    newDataQualityVO.setIntegrityRate(0);
                }else {
                    newDataQualityVO.setIntegrityRate(ArithmeticUtils.mul(ArithmeticUtils.div(sbCount,ybCount,3),100,3));
                }
                rateVoList.add(newDataQualityVO);
            }

        return rateVoList;

    }

    @Override
    public List<DataQualityVO> listStationDataWzlGroupYM(DataQualityVO dataQualityVOGroupYM) throws ParseException {
        List<DataQualityVO> selStationDataWzlGroupYM=  new ArrayList<>();
        //如果没有选择时间，默认为当前年月
        if (dataQualityVOGroupYM.getQueryTime()==null){
            dataQualityVOGroupYM.setQueryTime(new Date());
        }
        String [] strings= DateUtils.jsYmd(dataQualityVOGroupYM.getQueryTime());
        String yy =strings[0];
        dataQualityVOGroupYM.setYy(yy);
        //查询水管机构级别
        HashMap<String, Object> addvAndOrgCodeInfo = BusinessUtils.getAddvAndOrgCodeInfo(dataQualityVOGroupYM.getOrgCd());
        int grade = (int) addvAndOrgCodeInfo.get("grade");
        String resCode = (String) addvAndOrgCodeInfo.get("resCode");
        dataQualityVOGroupYM.setOrgCd(resCode + "%");
        resCode = resCode + "%";
        //查询水管机构的某年月统计实报数
        List<DataQualityVO> dataQualityVOList= dataQualityMapper.selStStationRSbCountGroupYM(dataQualityVOGroupYM);
        //查询水管机构所属所有测站
        List<DataQualityVO> stStationFreqList =dataQualityMapper.selStStationFreqByOrgCd(dataQualityVOGroupYM);
        //获取今年的所有月份
        List <String> dateAllList=DateUtils.getYearMonthBetween(dataQualityVOGroupYM.getQueryTime());
        List<String> dateList=new ArrayList<>();
        for (DataQualityVO vo:dataQualityVOList
        ) {

            DataQualityVO small =new DataQualityVO();
            small.setSbCount(vo.getSbCount());
            small.setYbCount(vo.getYbCount());
            small.setYmTime(vo.getYmTime());
            dateList.add(vo.getYmTime());
            selStationDataWzlGroupYM.add(small);
        }
        //查询出不在时间范围内的值，赋值为0
        dateAllList.removeAll(dateList);
        for (String s:dateAllList
        ) {
            DataQualityVO small =new DataQualityVO();
            small.setSbCount(0);
            small.setYbCount(0);
            small.setYmTime(s);
            selStationDataWzlGroupYM.add(small);
        }
        for (DataQualityVO d:selStationDataWzlGroupYM
             ) {
            Integer ybCount=0;
            for (DataQualityVO s:stStationFreqList
                 ) {
                ybCount+=countEQString(d.getYmTime(),s.getStatFreq());
            }
            d.setYbCount(ybCount);
            if (ybCount==0){
                d.setIntegrityRate(0);
            }else {
                d.setIntegrityRate(ArithmeticUtils.mul(ArithmeticUtils.div(d.getSbCount(),ybCount,3),100,3));
            }
        }
        selStationDataWzlGroupYM=selStationDataWzlGroupYM.stream().sorted(Comparator.comparing(DataQualityVO::getYmTime)).collect(Collectors.toList());

        return selStationDataWzlGroupYM;
    }

    @Override
    public List<DataQualityVO> listStationDataWzlGroupMD(DataQualityVO dataQualityVOGroupMD) throws ParseException {
        List<DataQualityVO> selStationDataWzlGroupMD=  new ArrayList<>();
        //如果没有选择时间，默认为当前年月
        if (dataQualityVOGroupMD.getQueryTime()==null){
            dataQualityVOGroupMD.setQueryTime(new Date());
        }
        String [] strings= DateUtils.jsYmd(dataQualityVOGroupMD.getQueryTime());
        String yy =strings[0];
        String mm =strings[1];
        dataQualityVOGroupMD.setYy(yy);
        dataQualityVOGroupMD.setMm(mm);
        //查询水管机构级别
        HashMap<String, Object> addvAndOrgCodeInfo = BusinessUtils.getAddvAndOrgCodeInfo(dataQualityVOGroupMD.getOrgCd());
        int grade = (int) addvAndOrgCodeInfo.get("grade");
        String resCode = (String) addvAndOrgCodeInfo.get("resCode");
        dataQualityVOGroupMD.setOrgCd(resCode + "%");
//        if (grade == 1) {
//            //查找下属水管机构
//            resCode = resCode + "%0000";
//        } else if (grade == 2) {
//            //查找下属水管机构
//            resCode = resCode + "%00";
//        } else if (grade == 3) {
//            resCode = resCode + "%";
//        } else {
//            resCode = resCode;
//        }
//        dataQualityVOGroupMD.setOrgCd(resCode);
        //查询水管机构所属所有测站
        List<DataQualityVO> stStationFreqList =dataQualityMapper.selStStationFreqByOrgCd(dataQualityVOGroupMD);
        List<DataQualityVO> dataQualityVOList =dataQualityMapper.selStStationRSbCountGroupMD(dataQualityVOGroupMD);
        //获取月的所有天
        List<String> dateAllList = DateUtils.getMonthDayBetween(dataQualityVOGroupMD.getQueryTime());
        List<String> dateList=new ArrayList<>();
        for (DataQualityVO vo:dataQualityVOList
        ) {

            DataQualityVO small =new DataQualityVO();
            small.setSbCount(vo.getSbCount());
            small.setYbCount(vo.getYbCount());
            small.setMdTime(vo.getMdTime());
            dateList.add(vo.getMdTime());
            selStationDataWzlGroupMD.add(small);
        }
        //查询出不在时间范围内的值，赋值为0
        dateAllList.removeAll(dateList);
        for (String s:dateAllList
        ) {
            DataQualityVO small =new DataQualityVO();
            small.setSbCount(0);
            small.setYbCount(0);
            small.setMdTime(s);
            selStationDataWzlGroupMD.add(small);
        }
        for (DataQualityVO d:selStationDataWzlGroupMD
        ) {
            Integer ybCount=0;
            for (DataQualityVO s:stStationFreqList
            ) {
                ybCount+=countEQString(d.getMdTime(),s.getStatFreq());
            }
            d.setYbCount(ybCount);
            if (ybCount==0){
                d.setIntegrityRate(0);
            }else {
                d.setIntegrityRate(ArithmeticUtils.mul(ArithmeticUtils.div(d.getSbCount(),ybCount,3),100,3));
            }
        }
        selStationDataWzlGroupMD=selStationDataWzlGroupMD.stream().sorted(Comparator.comparing(DataQualityVO::getMdTime)).collect(Collectors.toList());
        return selStationDataWzlGroupMD;
    }

    @Override
    public List<DataQualityVO> listPicStationDataWzl(DataQualityVO picDataQualityVO) throws ParseException {
        //如果没有选择时间，默认为当前年月
        if (picDataQualityVO.getQueryTime()==null){
            picDataQualityVO.setQueryTime(new Date());
        }
        String [] strings= DateUtils.jsYmd(picDataQualityVO.getQueryTime());
        String yy =strings[0];
        String mm =strings[1];
        picDataQualityVO.setYy(yy);
        picDataQualityVO.setMm(mm);

        //查询水管机构级别
        HashMap<String, Object> addvAndOrgCodeInfo = BusinessUtils.getAddvAndOrgCodeInfo(picDataQualityVO.getOrgCd());
        int grade = (int) addvAndOrgCodeInfo.get("grade");
        String resCode = (String) addvAndOrgCodeInfo.get("resCode");
        picDataQualityVO.setOrgCd(resCode + "%");
        if (grade == 1) {
            //查找下属水管机构
            resCode = resCode + "%0000";
        } else if (grade == 2) {
            //查找下属水管机构
            resCode = resCode + "%00";
        } else if (grade == 3) {
            resCode = resCode + "%";
        } else {
            resCode = resCode;
        }
        //根据水管机构和时间查询测站实报次数，上报频率
        List<DataQualityVO> dataQualityVOList= dataQualityMapper.selPicStStationRSbCount(picDataQualityVO);
        //查找下属机构
        RzsWrStatvolHwrVO rzsWrStatvolHwrVO=new RzsWrStatvolHwrVO();
        rzsWrStatvolHwrVO.setOrgCd(resCode);
        List<RzsWrStatvolHwrVO> orglist = wrStatvolHMapper.listOrgCd(rzsWrStatvolHwrVO);
        List<DataQualityVO> rateVoList=new ArrayList<>();
        for (RzsWrStatvolHwrVO r:orglist
        ) {
            HashMap<String, Object> stringObjectHashMap = BusinessUtils.getAddvAndOrgCodeInfo(r.getOrgCd());
            String ro =  (String) stringObjectHashMap.get("resCode");
            //过滤后只查所属水管机构的测站
            List<DataQualityVO> filterDataQualityVOList = dataQualityVOList.stream().filter(s -> s.getOrgCd().startsWith(ro)).collect(Collectors.toList());
            DataQualityVO newDataQualityVO=new DataQualityVO();
            int sbCount =0;
            int ybCount =0;
            for (DataQualityVO d:filterDataQualityVOList
            ) {
                sbCount+=d.getSbCount();
                //通过查询时间和上报频次计算应报次数
                ybCount+=countEQ(picDataQualityVO.getQueryTime(),d.getStatFreq());
            }
            newDataQualityVO.setSbCount(sbCount);
            newDataQualityVO.setYbCount(ybCount);
            newDataQualityVO.setStatCd(r.getStatCd());
            newDataQualityVO.setOrgCd(r.getOrgCd());
            newDataQualityVO.setOrgNm(r.getOrgNm());
            if (ybCount==0){
                newDataQualityVO.setIntegrityRate(0);
            }else {
                newDataQualityVO.setIntegrityRate(ArithmeticUtils.mul(ArithmeticUtils.div(sbCount,ybCount,3),100,3));
            }
            rateVoList.add(newDataQualityVO);
        }
        return  rateVoList;
    }

    @Override
    public List<DataQualityVO> listPicStationDataWzlGroupYM(DataQualityVO picDataQualityVOGroupYM) throws ParseException {
        List<DataQualityVO> selPicStationDataWzlGroupYM=  new ArrayList<>();
        //如果没有选择时间，默认为当前年月
        if (picDataQualityVOGroupYM.getQueryTime()==null){
            picDataQualityVOGroupYM.setQueryTime(new Date());
        }
        String [] strings= DateUtils.jsYmd(picDataQualityVOGroupYM.getQueryTime());
        String yy =strings[0];
        picDataQualityVOGroupYM.setYy(yy);
        //查询水管机构级别
        HashMap<String, Object> addvAndOrgCodeInfo = BusinessUtils.getAddvAndOrgCodeInfo(picDataQualityVOGroupYM.getOrgCd());
        int grade = (int) addvAndOrgCodeInfo.get("grade");
        String resCode = (String) addvAndOrgCodeInfo.get("resCode");
        picDataQualityVOGroupYM.setOrgCd(resCode + "%");
//        if (grade == 1) {
//            //查找下属水管机构
//            resCode = resCode + "%0000";
//        } else if (grade == 2) {
//            //查找下属水管机构
//            resCode = resCode + "%00";
//        } else if (grade == 3) {
//            resCode = resCode + "%";
//        } else {
//            resCode = resCode;
//        }
//        picDataQualityVOGroupYM.setOrgCd(resCode);
        //查询水管机构的某年月统计实报数
        List<DataQualityVO> dataQualityVOList= dataQualityMapper.selPicStStationRSbCountGroupYM(picDataQualityVOGroupYM);
        //查询水管机构所属所有测站
        List<DataQualityVO> stStationFreqList =dataQualityMapper.selStStationFreqByOrgCd(picDataQualityVOGroupYM);
        //获取今年的所有月份
        List <String> dateAllList=DateUtils.getYearMonthBetween(picDataQualityVOGroupYM.getQueryTime());
        List<String> dateList=new ArrayList<>();
        for (DataQualityVO vo:dataQualityVOList
        ) {

            DataQualityVO small =new DataQualityVO();
            small.setSbCount(vo.getSbCount());
            small.setYbCount(vo.getYbCount());
            small.setYmTime(vo.getYmTime());
            dateList.add(vo.getYmTime());
            selPicStationDataWzlGroupYM.add(small);
        }
        //查询出不在时间范围内的值，赋值为0
        dateAllList.removeAll(dateList);
        for (String s:dateAllList
        ) {
            DataQualityVO small =new DataQualityVO();
            small.setSbCount(0);
            small.setYbCount(0);
            small.setYmTime(s);
            selPicStationDataWzlGroupYM.add(small);
        }
        for (DataQualityVO d:selPicStationDataWzlGroupYM
        ) {
            Integer ybCount=0;
            for (DataQualityVO s:stStationFreqList
            ) {
                ybCount+=countEQString(d.getYmTime(),s.getStatFreq());
            }
            d.setYbCount(ybCount);
            if (ybCount==0){
                d.setIntegrityRate(0);
            }else {
                d.setIntegrityRate(ArithmeticUtils.mul(ArithmeticUtils.div(d.getSbCount(),ybCount,3),100,3));
            }
        }
        selPicStationDataWzlGroupYM=selPicStationDataWzlGroupYM.stream().sorted(Comparator.comparing(DataQualityVO::getYmTime)).collect(Collectors.toList());

        return selPicStationDataWzlGroupYM;
    }

    @Override
    public List<DataQualityVO> listPicStationDataWzlGroupMD(DataQualityVO picDataQualityVOGroupMD) throws ParseException {
        List<DataQualityVO> selStationDataWzlGroupMD=  new ArrayList<>();
        //如果没有选择时间，默认为当前年月
        if (picDataQualityVOGroupMD.getQueryTime()==null){
            picDataQualityVOGroupMD.setQueryTime(new Date());
        }
        String [] strings= DateUtils.jsYmd(picDataQualityVOGroupMD.getQueryTime());
        String yy =strings[0];
        String mm =strings[1];
        picDataQualityVOGroupMD.setYy(yy);
        picDataQualityVOGroupMD.setMm(mm);
        //查询水管机构级别
        HashMap<String, Object> addvAndOrgCodeInfo = BusinessUtils.getAddvAndOrgCodeInfo(picDataQualityVOGroupMD.getOrgCd());
        int grade = (int) addvAndOrgCodeInfo.get("grade");
        String resCode = (String) addvAndOrgCodeInfo.get("resCode");
        picDataQualityVOGroupMD.setOrgCd(resCode + "%");
//        if (grade == 1) {
//            //查找下属水管机构
//            resCode = resCode + "%0000";
//        } else if (grade == 2) {
//            //查找下属水管机构
//            resCode = resCode + "%00";
//        } else if (grade == 3) {
//            resCode = resCode + "%";
//        } else {
//            resCode = resCode;
//        }
//        picDataQualityVOGroupMD.setOrgCd(resCode);
        //查询水管机构所属所有测站
        List<DataQualityVO> stStationFreqList =dataQualityMapper.selStStationFreqByOrgCd(picDataQualityVOGroupMD);
        List<DataQualityVO> dataQualityVOList =dataQualityMapper.selPicStStationRSbCountGroupMD(picDataQualityVOGroupMD);
        //获取月的所有天
        List<String> dateAllList = DateUtils.getMonthDayBetween(picDataQualityVOGroupMD.getQueryTime());
        List<String> dateList=new ArrayList<>();
        for (DataQualityVO vo:dataQualityVOList
        ) {

            DataQualityVO small =new DataQualityVO();
            small.setSbCount(vo.getSbCount());
            small.setYbCount(vo.getYbCount());
            small.setMdTime(vo.getMdTime());
            dateList.add(vo.getMdTime());
            selStationDataWzlGroupMD.add(small);
        }
        //查询出不在时间范围内的值，赋值为0
        dateAllList.removeAll(dateList);
        for (String s:dateAllList
        ) {
            DataQualityVO small =new DataQualityVO();
            small.setSbCount(0);
            small.setYbCount(0);
            small.setMdTime(s);
            selStationDataWzlGroupMD.add(small);
        }
        for (DataQualityVO d:selStationDataWzlGroupMD
        ) {
            Integer ybCount=0;
            for (DataQualityVO s:stStationFreqList
            ) {
                ybCount+=countEQString(d.getMdTime(),s.getStatFreq());
            }
            d.setYbCount(ybCount);
            if (ybCount==0){
                d.setIntegrityRate(0);
            }else {
                d.setIntegrityRate(ArithmeticUtils.mul(ArithmeticUtils.div(d.getSbCount(),ybCount,3),100,3));
            }
        }
        selStationDataWzlGroupMD=selStationDataWzlGroupMD.stream().sorted(Comparator.comparing(DataQualityVO::getMdTime)).collect(Collectors.toList());
        return selStationDataWzlGroupMD;
    }

    @Override
    public List<DataQualityVO> listEquipmentAlarmBjl(DataQualityVO eqDataQualityVO) throws ParseException {

        //如果没有选择时间，默认为当前年月
        if (eqDataQualityVO.getQueryTime()==null){
            eqDataQualityVO.setQueryTime(new Date());
        }
        String [] strings= DateUtils.jsYmd(eqDataQualityVO.getQueryTime());
        String yy =strings[0];
        String mm =strings[1];
        eqDataQualityVO.setYy(yy);
        eqDataQualityVO.setMm(mm);

        //查询水管机构级别
        HashMap<String, Object> addvAndOrgCodeInfo = BusinessUtils.getAddvAndOrgCodeInfo(eqDataQualityVO.getOrgCd());
        int grade = (int) addvAndOrgCodeInfo.get("grade");
        String resCode = (String) addvAndOrgCodeInfo.get("resCode");
        eqDataQualityVO.setOrgCd(resCode + "%");
        if (grade == 1) {
            //查找下属水管机构
            resCode = resCode + "%0000";
        } else if (grade == 2) {
            //查找下属水管机构
            resCode = resCode + "%00";
        } else if (grade == 3) {
            resCode = resCode + "%";
        } else {
            resCode = resCode;
        }

        //根据水管机构和时间查询测站实报次数，上报频率
        List<DataQualityVO> dataQualityVOList= dataQualityMapper.selEquipmentAlarmBjlCount(eqDataQualityVO);
        //查找下属机构
        RzsWrStatvolHwrVO rzsWrStatvolHwrVO=new RzsWrStatvolHwrVO();
        rzsWrStatvolHwrVO.setOrgCd(resCode);
        List<RzsWrStatvolHwrVO> orglist = wrStatvolHMapper.listOrgCd(rzsWrStatvolHwrVO);
        List<DataQualityVO> rateVoList=new ArrayList<>();
        for (RzsWrStatvolHwrVO r:orglist
        ) {
            HashMap<String, Object> stringObjectHashMap = BusinessUtils.getAddvAndOrgCodeInfo(r.getOrgCd());
            String ro =  (String) stringObjectHashMap.get("resCode");
            //过滤后只查所属水管机构的测站
            List<DataQualityVO> filterDataQualityVOList = dataQualityVOList.stream().filter(s -> s.getOrgCd().startsWith(ro)).collect(Collectors.toList());
            DataQualityVO newDataQualityVO=new DataQualityVO();
            newDataQualityVO.setOrgCd(r.getOrgCd());
            newDataQualityVO.setOrgNm(r.getOrgNm());
          //  int cellVoltNormal =0;
            int cellVoltAbNormal =0;
            int cellVoltAllNormal =0;
           // int signalNormal =0;
            int signalAbNormal =0;
            int signalAllNormal =0;
            for (DataQualityVO d:filterDataQualityVOList
            ) {
            //   cellVoltNormal +=d.getCellNormal();
               cellVoltAbNormal+=d.getCellAbnormal();
               cellVoltAllNormal+=d.getCellAllnormal();
             //  signalNormal+=d.getSignalNormal();
               signalAbNormal+=d.getSignalAbnormal();
               signalAllNormal+=d.getSignalAllnormal();
            }
            if (signalAllNormal==0){
                newDataQualityVO.setCellAlarmBjl(0);
                newDataQualityVO.setSignalAlarmBjl(0);
            }else {
                newDataQualityVO.setCellAlarmBjl(ArithmeticUtils.mul(ArithmeticUtils.div(cellVoltAbNormal,cellVoltAllNormal,3),100,3));
                newDataQualityVO.setSignalAlarmBjl(ArithmeticUtils.mul(ArithmeticUtils.div(signalAbNormal,signalAllNormal,3),100,3));
            }
            rateVoList.add(newDataQualityVO);
        }
        return  rateVoList;
    }

    @Override
    public List<DataQualityVO> listEqDataQualityVOGroupYM(DataQualityVO eqDataQualityVOGroupYM) throws ParseException {
        List<DataQualityVO> selEqDataQualityVOGroupYM=  new ArrayList<>();
        //如果没有选择时间，默认为当前年月
        if (eqDataQualityVOGroupYM.getQueryTime()==null){
            eqDataQualityVOGroupYM.setQueryTime(new Date());
        }
        String [] strings= DateUtils.jsYmd(eqDataQualityVOGroupYM.getQueryTime());
        String yy =strings[0];
        eqDataQualityVOGroupYM.setYy(yy);
        //查询水管机构级别
        HashMap<String, Object> addvAndOrgCodeInfo = BusinessUtils.getAddvAndOrgCodeInfo(eqDataQualityVOGroupYM.getOrgCd());
        int grade = (int) addvAndOrgCodeInfo.get("grade");
        String resCode = (String) addvAndOrgCodeInfo.get("resCode");
        eqDataQualityVOGroupYM.setOrgCd(resCode + "%");
//        if (grade == 1) {
//            //查找下属水管机构
//            resCode = resCode + "%0000";
//        } else if (grade == 2) {
//            //查找下属水管机构
//            resCode = resCode + "%00";
//        } else if (grade == 3) {
//            resCode = resCode + "%";
//        } else {
//            resCode = resCode;
//        }
//        eqDataQualityVOGroupYM.setOrgCd(resCode);
        //查询水管机构的某年月统计实报数
        List<DataQualityVO> dataQualityVOList= dataQualityMapper.selEquipmentAlarmBjlCountGroupYM(eqDataQualityVOGroupYM);
        //获取今年的所有月份
        List <String> dateAllList=DateUtils.getYearMonthBetween(eqDataQualityVOGroupYM.getQueryTime());
        List<String> dateList=new ArrayList<>();
        for (DataQualityVO vo:dataQualityVOList
        ) {

            DataQualityVO small =new DataQualityVO();
            small.setCellAbnormal(vo.getCellAbnormal());
            small.setCellAllnormal(vo.getCellAllnormal());
            small.setSignalAbnormal(vo.getSignalAbnormal());
            small.setSignalAllnormal(vo.getSignalAllnormal());
            small.setYmTime(vo.getYmTime());
            dateList.add(vo.getYmTime());
            selEqDataQualityVOGroupYM.add(small);
        }
        //查询出不在时间范围内的值，赋值为0
        dateAllList.removeAll(dateList);
        for (String s:dateAllList
        ) {
            DataQualityVO small =new DataQualityVO();
            small.setCellAbnormal(0);
            small.setCellAllnormal(0);
            small.setSignalAbnormal(0);
            small.setSignalAllnormal(0);
            small.setYmTime(s);
            selEqDataQualityVOGroupYM.add(small);
        }
        for (DataQualityVO d:selEqDataQualityVOGroupYM
        ) {
            int cellVoltAbNormal =0;
            int cellVoltAllNormal =0;
            // int signalNormal =0;
            int signalAbNormal =0;
            int signalAllNormal =0;
            cellVoltAbNormal+=d.getCellAbnormal();
            cellVoltAllNormal+=d.getCellAllnormal();
            //  signalNormal+=d.getSignalNormal();
            signalAbNormal+=d.getSignalAbnormal();
            signalAllNormal+=d.getSignalAllnormal();

            if (signalAllNormal==0){
                d.setCellAlarmBjl(0);
                d.setSignalAlarmBjl(0);
            }else {
                d.setCellAlarmBjl(ArithmeticUtils.mul(ArithmeticUtils.div(cellVoltAbNormal,cellVoltAllNormal,3),100,3));
                d.setSignalAlarmBjl(ArithmeticUtils.mul(ArithmeticUtils.div(signalAbNormal,signalAllNormal,3),100,3));
            }
        }
        selEqDataQualityVOGroupYM=selEqDataQualityVOGroupYM.stream().sorted(Comparator.comparing(DataQualityVO::getYmTime)).collect(Collectors.toList());

        return selEqDataQualityVOGroupYM;
    }

    @Override
    public List<DataQualityVO> listEqDataQualityVOGroupMD(DataQualityVO eqDataQualityVOGroupMD) throws ParseException {
        List<DataQualityVO> selEqDataQualityVOGroupMD=  new ArrayList<>();
        //如果没有选择时间，默认为当前年月
        if (eqDataQualityVOGroupMD.getQueryTime()==null){
            eqDataQualityVOGroupMD.setQueryTime(new Date());
        }
        String [] strings= DateUtils.jsYmd(eqDataQualityVOGroupMD.getQueryTime());
        String yy =strings[0];
        String mm =strings[1];
        eqDataQualityVOGroupMD.setYy(yy);
        eqDataQualityVOGroupMD.setMm(mm);
        //查询水管机构级别
        HashMap<String, Object> addvAndOrgCodeInfo = BusinessUtils.getAddvAndOrgCodeInfo(eqDataQualityVOGroupMD.getOrgCd());
        int grade = (int) addvAndOrgCodeInfo.get("grade");
        String resCode = (String) addvAndOrgCodeInfo.get("resCode");
        eqDataQualityVOGroupMD.setOrgCd(resCode + "%");
//        if (grade == 1) {
//            //查找下属水管机构
//            resCode = resCode + "%0000";
//        } else if (grade == 2) {
//            //查找下属水管机构
//            resCode = resCode + "%00";
//        } else if (grade == 3) {
//            resCode = resCode + "%";
//        } else {
//            resCode = resCode;
//        }
//        eqDataQualityVOGroupMD.setOrgCd(resCode);
        //查询水管机构所属所有测站
        List<DataQualityVO> dataQualityVOList =dataQualityMapper.selEquipmentAlarmBjlCountGroupMD(eqDataQualityVOGroupMD);
        //获取月的所有天
        List<String> dateAllList = DateUtils.getMonthDayBetween(eqDataQualityVOGroupMD.getQueryTime());
        List<String> dateList=new ArrayList<>();
        for (DataQualityVO vo:dataQualityVOList
        ) {

            DataQualityVO small =new DataQualityVO();
            small.setCellAbnormal(vo.getCellAbnormal());
            small.setCellAllnormal(vo.getCellAllnormal());
            small.setSignalAbnormal(vo.getSignalAbnormal());
            small.setSignalAllnormal(vo.getSignalAllnormal());
            small.setMdTime(vo.getMdTime());
            dateList.add(vo.getMdTime());
            selEqDataQualityVOGroupMD.add(small);
        }
        //查询出不在时间范围内的值，赋值为0
        dateAllList.removeAll(dateList);
        for (String s:dateAllList
        ) {
            DataQualityVO small =new DataQualityVO();
            small.setCellAbnormal(0);
            small.setCellAllnormal(0);
            small.setSignalAbnormal(0);
            small.setSignalAllnormal(0);
            small.setMdTime(s);
            selEqDataQualityVOGroupMD.add(small);
        }
        for (DataQualityVO d:selEqDataQualityVOGroupMD
        ) {
            int cellVoltAbNormal =0;
            int cellVoltAllNormal =0;
            // int signalNormal =0;
            int signalAbNormal =0;
            int signalAllNormal =0;
            cellVoltAbNormal+=d.getCellAbnormal();
            cellVoltAllNormal+=d.getCellAllnormal();
            signalAbNormal+=d.getSignalAbnormal();
            signalAllNormal+=d.getSignalAllnormal();
            if (signalAllNormal==0){
                d.setCellAlarmBjl(0);
                d.setSignalAlarmBjl(0);
            }else {
                d.setCellAlarmBjl(ArithmeticUtils.mul(ArithmeticUtils.div(cellVoltAbNormal,cellVoltAllNormal,3),100,3));
                d.setSignalAlarmBjl(ArithmeticUtils.mul(ArithmeticUtils.div(signalAbNormal,signalAllNormal,3),100,3));
            }
        }
        selEqDataQualityVOGroupMD=selEqDataQualityVOGroupMD.stream().sorted(Comparator.comparing(DataQualityVO::getMdTime)).collect(Collectors.toList());
        return selEqDataQualityVOGroupMD;
    }

    /**
     * 应报次数：	无线
     * 雷达遥测水位计	每5分钟1次 2
     * 	雷达水位计	每小时1次  1
     * 	电子水尺	每5分钟1次  3
     * @param date 时间
     * @return
     */
    private Integer countEQString(String date,Integer sbcount) throws ParseException {
        Long s=jsStringDate(date,sbcount);
        return s.intValue();
    }

    /**
     * 根据时间、上报频率计算应报次数
     * @param date
     * @param sbcount
     * @return
     * @throws ParseException
     */
    private static Long jsStringDate(String date,Integer sbcount) throws ParseException {
        long s = 0;
        if (sbcount==0){
            return s;
        }
        int n = date.length()-date.replaceAll("-", "").length();
        Date startDate = new Date();
        Date endDate = new Date();
        //计算当天开始到结束的应报次数
        SimpleDateFormat startSdf = new SimpleDateFormat("yyyy-MM-dd");
        if (n==1){
            String dates[] =date.split("-");

            //获得当前月第一天
            startDate=DateUtils.getDay(startSdf.parse(DateUtils.getFirstDayOfMonth1(Integer.parseInt(dates[0]),Integer.parseInt(dates[1]))));
            //获得当前月最后一天
            endDate = DateUtils.getDay(startSdf.parse(DateUtils.getLastDayOfMonth1(Integer.parseInt(dates[0]),Integer.parseInt(dates[1]))));
        }else {
            //获得当前月第一天
            startDate =DateUtils.getDay(startSdf.parse(date));
            //获得当前月最后一天
            endDate = DateUtils.getDayEnd(startSdf.parse(date));
        }

        SimpleDateFormat Sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        String start = Sdf.format(startDate);
        String end = Sdf.format(endDate);
        if (!endDate.before(new Date())){
            //说明时间已超过当前时间，设置为当前时间
            Date nowDate=new Date();
            Calendar calendarnow = Calendar.getInstance();
            calendarnow.setTime(nowDate);
            end= Sdf.format(calendarnow.getTime());
            s=1;
        }
        long time1 = DateUtils.dateLongToStamp(start);
        long time2 =DateUtils.dateLongToStamp(end);
        s = s+(time2 - time1) / (1000 * 60) / sbcount;
        if (s<0){
            s=0;
        }
        return s;
    }


    /**
     * 应报次数：	无线雷达遥测水位计	每5分钟1次 2
     * 	雷达水位计	每小时1次  1
     * 	电子水尺	每5分钟1次  3
     * @param date 时间
     * @return
     */
    private static Integer countEQ(Date date,Integer sbcount) throws ParseException {
        Long s=jsdate(date,sbcount);
        return s.intValue();
    }

    /**
     * 根据时间、上报频率计算应报次数
     * @param date
     * @param sbcount
     * @return
     * @throws ParseException
     */
    private static Long jsdate(Date date,Integer sbcount) throws ParseException {
        long s = 0;
        if (sbcount==0){
            return s;
        }
        //计算当前月开始到结束的应报次数
        SimpleDateFormat startSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //获得当前月第一天
        Date startDate =DateUtils.getMonthStart(date);
        //获得当前月最后一天
        Date endDate = DateUtils.getMonthEnd(date);
        SimpleDateFormat endSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String start = startSdf.format(startDate);
        String end = endSdf.format(endDate);
        if (!endDate.before(new Date())){
            //说明时间已超过当前时间，设置为当前时间之后的1小时
            Date nowDate=new Date();
            Calendar calendarnow = Calendar.getInstance();
            calendarnow.setTime(nowDate);
            end= startSdf.format(calendarnow.getTime());
            s=1;
        }
        long time1 = DateUtils.dateLongToStamp(start);
        long time2 =DateUtils.dateLongToStamp(end);

        s = s+(time2 - time1) / (1000 * 60) / sbcount;
        if (s<0){
            s=0;
        }
        return s;
    }


    public static void main(String[] args) throws ParseException {
       System.out.println(countEQ(new Date(),60));
    }


}
